home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / newmarch.zip / POPUPEDI.C < prev    next >
C/C++ Source or Header  |  1992-09-08  |  5KB  |  208 lines

  1. /* Author:   $Author: jan $
  2.  * File:     $Source: /usr/usrs/jan/desktop/X_Book.boo/programs/RCS/popupedit.c,v $
  3.  * Date:     $Date: 1992/09/09 00:10:06 $
  4.  * Revision: $Revision: 1.1 $
  5.  */
  6.  
  7. #include "copyright.h"
  8.  
  9. #include <stdio.h>
  10. #include <Xm/PushB.h>   
  11. #include <Xm/Text.h>    
  12. #include <Xm/SelectioB.h>       
  13.         
  14. #define MAX_ARGS 20
  15.  
  16. #if XmVersion == 1000
  17. #define MOTIF_1_0
  18. #endif
  19.  
  20. char Class_name[] ="XmPopupEditor";     
  21.  
  22. Widget edit_dialog;     
  23.  
  24. char *in_text  = "Edit Me";
  25. char *out_text = NULL;
  26.  
  27. #ifdef MOTIF_1_0
  28. /* to fix a "feature" of a bulletin board in a dialog */
  29. char transLine[] = "<Key>Return: newline()";
  30. #endif
  31.  
  32. /*------------------------------------------------------
  33. **      Save
  34. */
  35. void edit_saveCB(w, out_text, call_data)
  36. Widget w;
  37. char **out_text;
  38. caddr_t call_data;
  39. {
  40.     Widget  scrolled_window, edit_text;
  41.     Arg    args[MAX_ARGS];
  42.  
  43.     /* reclaim space from the old string */
  44.     XtFree (*out_text);
  45.     
  46.     /* find the scrolled window */
  47.     /* find the text widget */
  48.     scrolled_window = XmSelectionBoxGetChild (w,
  49.                      XmDIALOG_WORK_AREA);
  50.     /* the text widget is the scrolled window's
  51.        work area child. Get the child from the
  52.        workWindow resource */
  53.     XtSetArg (args[0], XmNworkWindow, &edit_text);
  54.     XtGetValues (scrolled_window, args, 1);
  55.  
  56.     /* get the text string */
  57.     *out_text = XmTextGetString(edit_text);
  58. }
  59.  
  60. void edit_helpCB(w, client_data, call_data)
  61.     Widget w;
  62.     caddr_t client_data;
  63.     caddr_t call_data;
  64. {
  65.     /* Should use the CreateHelp routine of earlier.
  66.        This is a pretty stupid method, guaranteed to
  67.       annoy users:
  68.     */
  69.     fprintf(stderr, "help not implemented\n");
  70. }
  71.  
  72. void edit_cancelCB(w, text, call_data)
  73.     Widget  w;
  74.     char    **text;
  75.     caddr_t call_data;
  76. {
  77.     Widget shell = XtParent(edit_dialog);
  78.  
  79.     *text = NULL;
  80.     printf("Edit dialog cancelled\n");
  81.     XtUnmanageChild(edit_dialog);
  82.     XtDestroyWidget(shell);
  83. }
  84.  
  85. void
  86. edit_quitCB(w, text, call_data)
  87.     Widget  w;
  88.     char    **text;
  89.     caddr_t call_data;
  90. {
  91.     Widget shell = XtParent(edit_dialog);
  92.  
  93.     printf("Quitting popup edit dialog.\n");
  94.     if (*text == NULL)
  95.         printf("no edited text saved\n");
  96.     else
  97.         printf("Edited text was:\n\n%s", *text);
  98.     XtUnmanageChild(w);
  99.     XtDestroyWidget(shell);
  100. }
  101.  
  102. PopupEdit(parent, in_text, out_text)
  103.     Widget parent;
  104.     char    *in_text;
  105.     char    **out_text;
  106. {
  107.     int n;
  108. #ifdef MOTIF_1_0
  109.     XtTranslations transTable;
  110. #endif
  111.     Arg args[MAX_ARGS];
  112.     Widget w;
  113.     Widget edit_text;
  114.     /* create the display window with
  115.     ** a text widget as
  116.     ** a popup window
  117.     */
  118.     n = 0;
  119.     XtSetArg(args[n], XmNautoUnmanage, False); n++;
  120.     edit_dialog = XmCreatePromptDialog(parent,
  121.                         "Popup Editor", args, n);
  122.     XtAddCallback (edit_dialog, XmNapplyCallback, 
  123.                         edit_saveCB, out_text);
  124.     XtAddCallback (edit_dialog, XmNcancelCallback, 
  125.                         edit_cancelCB, out_text);
  126.     XtAddCallback (edit_dialog, XmNhelpCallback, 
  127.                         edit_helpCB, NULL);
  128.     XtAddCallback (edit_dialog, XmNokCallback, 
  129.                         edit_quitCB, out_text);
  130.  
  131.     n = 0;
  132.     XtSetArg(args[n], XmNscrollingPolicy,
  133.                   XmAUTOMATIC); n++;
  134.     XtSetArg(args[n], XmNeditMode,
  135.                   XmMULTI_LINE_EDIT); n++;
  136.     edit_text = XmCreateScrolledText(edit_dialog,
  137.                           "edit text", 
  138.                           args, n);
  139.     XmTextSetString (edit_text, in_text);
  140.     XtManageChild(edit_text);
  141.  
  142. #ifdef MOTIF_1_0
  143.     /* edit_text is a child of a selection box
  144.        in a dialog.
  145.        A form is a subclass of a bulletin board which
  146.        "redefines" the return key in this context.
  147.        Fix the return key back to what it should be:
  148.     */
  149.     transTable = XtParseTranslationTable(transLine);
  150.     XtOverrideTranslations(edit_text, transTable);
  151. #endif
  152.  
  153.     /* Show the apply button
  154.     */
  155.     w = XmSelectionBoxGetChild (edit_dialog, 
  156.                     XmDIALOG_APPLY_BUTTON);
  157.     XtManageChild (w);    
  158.     /* and lose the input area
  159.     */
  160.     w = XmSelectionBoxGetChild (edit_dialog, 
  161.                         XmDIALOG_TEXT);
  162.     XtUnmanageChild (w);
  163.     
  164.     XtManageChild(edit_dialog);
  165. }
  166.  
  167. void
  168. PushCB(w, client_data, call_data)
  169. Widget w;
  170. caddr_t client_data;
  171. caddr_t call_data;
  172. {       
  173.     PopupEdit (w, in_text, &out_text);
  174. }
  175.     
  176. main(argc, argv)    
  177. int argc;
  178. char **argv;
  179. {       
  180.     Widget toplevel;
  181.     Widget button;
  182.         
  183.     /* Initialize the intrinsics  
  184.        with a toplevel widget */ 
  185.     toplevel = XtInitialize(NULL,
  186.                      Class_name,     
  187.                      NULL,        
  188.                      0,        
  189.                      &argc, argv);    
  190.  
  191.   /* Create a widget, with the
  192.        toplevel as manager;
  193.     */      
  194.     button = XmCreatePushButton(toplevel, 
  195.                         "Edit Me",
  196.                         NULL,
  197.                         0);
  198.     XtAddCallback (button, XmNactivateCallback,
  199.                        PushCB);
  200.     XtManageChild (button);
  201.         
  202.     /* display all of the widgets */    
  203.     XtRealizeWidget(toplevel);      
  204.     
  205.     /* enter the main processing loop */    
  206.     XtMainLoop();
  207. }
  208.